package com.it.btyw.modules.welcome

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.it.btyw.modules.main.activity.MainActivity
import com.it.btyw.modules.user.activity.LoginActivity
import com.it.common.utils.ActivityUtils
import com.it.common.utils.DisplayUtil
import com.it.common.utils.LogUtil
import com.it.data.manager.AppManager
import kotlin.system.exitProcess

/**
 *  @Description：欢迎页
 *  @author：weishixiong
 *  @date：2023/3/28 15:55
 *
 */
class WelcomeActivity : AppCompatActivity() {


    companion object {
        const val TAG = "WelcomeActivity"
    }

    //状态栏，导航栏的设置
    private fun initStatus() {
        val actionBar = supportActionBar
        actionBar?.hide()
        // 隐藏状态栏和导航栏
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN or
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        // 设置全屏
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(javaClass.simpleName, "onCreate: ${javaClass.simpleName}")
        initStatus()
        initView()
        ActivityUtils.finishAllExit(javaClass.simpleName)
        ActivityUtils.addActivity(this)
    }


    override fun onDestroy() {
        super.onDestroy()
        ActivityUtils.removeActivity(this)
    }

    override fun onResume() {
        super.onResume()
        initPermissions()

    }

    private fun initView() {
        val screenHeight = DisplayUtil.getScreenHeight(this)
        val screenWidth = DisplayUtil.getScreenWidth(this)
        LogUtil.d(TAG, "initView: screenHeight = ${screenHeight} and screenWidth = $screenWidth")
        val code = AppManager.getDeviceCode(this)
        Log.d(TAG, "initView: code ${code}")

    }

    /**
     * 统一授权，一次性申请
     */
    private fun initPermissions() {
        //安卓11需要申请读写权限
        XXPermissions.with(this) // 不适配 Android 11 可以这样写
            //.permission(Permission.Group.STORAGE)
            // 适配 Android 11 需要这样写，这里无需再写 Permission.Group.STORAGE
            .permission(
                Permission.MANAGE_EXTERNAL_STORAGE,
                Permission.RECORD_AUDIO,
            )
            .request { _, allGranted ->
                LogUtil.d(TAG, "onGranted: $allGranted")
                if (allGranted) {
                    val intent = Intent(this, LoginActivity::class.java)
                    startActivity(intent)
                } else {
                    finish()
                    exitProcess(0)
                }
            }
    }


    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart: ============")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop: ============")
    }

    override fun onRestart() {
        super.onRestart()
        Log.d(TAG, "onRestart: ============")
    }


}